package GUI;

import jsonObjects.Profile;
import org.apache.log4j.Logger;

import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;

import serverHandling.SessionObjects;

import javax.swing.*;

import java.util.List;

public class FileAttributesProfilesGUI extends JFrame {
    private static Logger log = Logger.getLogger(FileAttributesProfilesGUI.class);

    public FileAttributesProfilesGUI() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
	try {
	    UIManager
		    .setLookAndFeel(new Plastic3DLookAndFeel());
	} catch (Exception e) {
	    log.error(e.getMessage());
	}
        List<Profile> activProfTab = SessionObjects.getProfiles();
        Object[][] futureMenuItems;
        if (activProfTab != null) {
            futureMenuItems = new Object[activProfTab.size()][activProfTab.size()];
            for (int i = 0; i < activProfTab.size(); i++) {
                futureMenuItems[i][0] = activProfTab.get(i).getName();
            }
        } else
            futureMenuItems = new Object[0][0];

        jScrollPane1 = new JScrollPane();
        jTable1 = new JTable();
        jButton1 = new JButton();
        jButton2 = new JButton();

        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                futureMenuItems,
                new String[]{
                        "Nazwa"
                }
        ) {
            Class[] types = new Class[]{
                    String.class
            };
            boolean[] canEdit = new boolean[]{
                    false
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jTable1.setColumnSelectionAllowed(true);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTable1.getColumnModel().getColumn(0).setResizable(false);

        jButton1.setText("Lista plikow");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jButton2)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButton1))
                                        .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 375, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 240, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton1)
                                        .addComponent(jButton2))
                                .addContainerGap(15, Short.MAX_VALUE))
        );

        jButton1.getAccessibleContext().setAccessibleName("okButton");
        jButton2.getAccessibleContext().setAccessibleName("anulujButton");

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        if (jTable1.getSelectedRowCount() == 1) {

            int row = jTable1.getSelectedRow();
            int column = jTable1.getSelectedColumn();
            String urObjctInCell = (String) jTable1.getValueAt(row, column);
            final Profile profTmp = SessionObjects.getProfileByName(urObjctInCell);

            if (profTmp == null)
                JOptionPane.showMessageDialog(null, "Nie odnaleziona atrybutu dla wybranego profilu!");

            else {
        	this.setVisible(false);
                java.awt.EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        new FileAttributeProfileFileListGUI(profTmp).setVisible(true);
                    }
                });
            }

        } else {
            JOptionPane.showMessageDialog(null, "Nalezy wybrac profil!");
        }
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FileAttributesProfilesGUI().setVisible(true);
            }
        });
    }

    JButton jButton1;
    JButton jButton2;
    JScrollPane jScrollPane1;
    private JTable jTable1;
}
